package cn.com.bluemoon.daps.standard.controller.api;

import cn.com.bluemoon.daps.common.annotation.FcApiFor;
import cn.com.bluemoon.daps.common.constant.DapConstant;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiStandard;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.toolkit.TreeUtils;
import cn.com.bluemoon.daps.standard.api.RemoteSystemCategoryService;
import cn.com.bluemoon.daps.standard.entity.DapSystemCategory;
import cn.com.bluemoon.daps.standard.mapper.DapSystemCategoryMapper;
import cn.com.bluemoon.daps.standard.service.DapSystemCategoryService;
import cn.com.bluemoon.daps.standard.vo.SysTopicCategorySimpleVo;
import cn.com.bluemoon.daps.standard.vo.SystemCategoryTreeVo;
import cn.com.bluemoon.daps.standard.vo.TopicCategoryVo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务接口生产类-内部调用
 *
 * @author Jarod.Kong
 */
@Api(tags = {"FeignClient服务-标准主题分类服务注入Api"})
@RestController
@RequestMapping(DapsApiStandard.API_SYS_CATEGORY)
@BmPermission(value = "", name = "服务注册-标准主题分类", ignore = true)
public class DapsSysCategoryApi implements RemoteSystemCategoryService {
    @Resource
    private DapSystemCategoryService systemCategoryService;

    @GetMapping("/category")
    @FcApiFor(team = {"数据资产团队", "数据工具团队"})
    public ResultBean<List<DapSystemCategory>> getSystemCategories(@RequestParam("ids") List<String> ids) {
        List<DapSystemCategory> r = Lists.newArrayList();
        if (ids != null) {
            r = systemCategoryService.getBaseMapper(DapSystemCategoryMapper.class).selectBatchIds(ids);
        }
        return ResultBean.ok(r);

    }

    @GetMapping("/sysCategories")
    public ResultBean<Map<String, List<DapSystemCategory>>> sysCategories() {
        // 主题分类
        List<DapSystemCategory> systemTopics = systemCategoryService.findTopicsByLevel(0);
        List<DapSystemCategory> systemCategories = systemCategoryService.systemTopicAndCategories("0");
        Map<String, List<DapSystemCategory>> map = Maps.newHashMap();
        map.put(DapConstant.DapStandard.SYSTEM_TOPIC2_NAME, systemTopics);
        map.put(DapConstant.DapStandard.SYSTEM_CATEGORY1_NAME, systemCategories);
        return ResultBean.ok(map);
    }

    @Override
    public ResultBean<TopicCategoryVo> getById(String cId) {
        return ResultBean.ok(systemCategoryService.getById(cId))
                .convert(TopicCategoryVo::new);
    }

    @Override
    @GetMapping("/tree")
    public ResultBean<List<SystemCategoryTreeVo>> categoryTree() {
        List<DapSystemCategory> res = systemCategoryService.list(systemCategoryService.bmLambdaQuery().in(DapSystemCategory::getTopic, 0, 1));
        List<SystemCategoryTreeVo> treeVos = res.stream().map(SystemCategoryTreeVo::new).collect(Collectors.toList());
        List<SystemCategoryTreeVo> trees = TreeUtils.createTrees(treeVos, "id", "parentId", "nextNodes", null, null);
        return ResultBean.ok(trees);
    }

    @Override
    public ResultBean<List<SysTopicCategorySimpleVo>> sysTopicCategories() {
        List<SysTopicCategorySimpleVo> res = systemCategoryService.getBaseMapper(DapSystemCategoryMapper.class).topicCategories();
        return ResultBean.ok(res);
    }

}
